question_bank = {}


def question_1():
    from datetime import datetime

    def day_of_year(year, month, day):
        date = datetime(year, month, day)
        day_num = date.timetuple().tm_yday
        return day_num

    year = int(input("请输入年份: "))
    month = int(input("请输入月份: "))
    day = int(input("请输入日期: "))

    print(f"{year}年{month}月{day}日是这一年的第{day_of_year(year, month, day)}天。")
question_bank["1"] = question_1


def question_3():
    def is_prime(n):
        if n <= 1:
            return False
        for i in range(2, int(n ** 0.5) + 1):
            if n % i == 0:
                return False
        return True

    def find_primes_in_range(limit):
        primes = []
        for num in range(2, limit + 1):
            if is_prime(num):
                primes.append(num)
        return primes

    limit = 1000

    primes = find_primes_in_range(limit)
    print(f"在1到{limit}之间有{len(primes)}个素数:")
    print(primes)
question_bank["3"] = question_3


def question_5():
    def get_grade(score):
        return 'A' if score >= 90 else ('B' if 60 <= score < 90 else 'C')

    try:
        score = float(input("请输入学习成绩（0-100）："))

        if not (0 <= score <= 100):
            print("分数必须在0到100之间。")
        else:
            grade = get_grade(score)
            print(f"分数: {score}, 等级: {grade}")
    except ValueError:
        print("输入无效，请输入一个数字。")
question_bank["5"] = question_5

def question_7():
    def count_characters(input_string):
        letters = 0
        spaces = 0
        digits = 0
        others = 0

        for char in input_string:
            if char.isalpha():
                letters += 1
            elif char.isspace():
                spaces += 1
            elif char.isdigit():
                digits += 1
            else:
                others += 1

        return letters, spaces, digits, others

    input_string = input("请输入一行字符串：")

    letters, spaces, digits, others = count_characters(input_string)

    print(f"英文字母个数: {letters}")
    print(f"空格个数: {spaces}")
    print(f"数字个数: {digits}")
    print(f"其他字符个数: {others}")
question_bank["7"] = question_7

def question_9():
    def find_perfect_numbers(limit):
        perfect_numbers = []
        for num in range(1, limit + 1):
            factors_sum = sum(i for i in range(1, num) if num % i == 0)
            if factors_sum == num:
                perfect_numbers.append(num)
        return perfect_numbers

    limit = 1000

    perfect_numbers = find_perfect_numbers(limit)
    print(f"1到{limit}之间的完数有：{perfect_numbers}")
question_bank["9"] = question_9

def question_11():
    def factorial(n):
        if n == 0 or n == 1:
            return 1
        else:
            return n * factorial(n - 1)

    def sum_of_factorials(N):
        total = 0
        for i in range(1, N + 1):
            total += factorial(i)
        return total

    try:
        N = int(input("请输入一个正整数N："))
        if N < 0:
            raise ValueError("N必须是一个非负整数。")
        
        result = sum_of_factorials(N)
        print(f" 阶乘结果为{result}")
    except ValueError as e:
        print(f"输入无效：{e}")
question_bank["11"] = question_11

def question_13():
    def print_diamond(N):
        """打印一个由奇数N控制的菱形图案"""
        if N % 2 == 0:
            print("N必须是一个奇数。")
            return
        
        
        for i in range(1, N + 1):
            if i <= (N + 1) // 2: 
                stars = 2 * i - 1
            else:
                stars = 2 * (N - i + 1) - 1 
            
            spaces = (N - stars) // 2
            
            print(' ' * spaces + '*' * stars)

    try:
        N = int(input("请输入一个奇数N："))
        if N <= 0 or N % 2 == 0:
            raise ValueError("N必须是一个正奇数。")
        
        print_diamond(N)
    except ValueError as e:
        print(f"输入无效：{e}")
question_bank["13"] = question_13

def question_15():
    import random

    numbers = [random.randint(1, 100) for _ in range(10)]

    print("原始数字列表:", numbers)

    sorted_numbers = sorted(numbers)
    print("使用sorted()函数排序后的列表:", sorted_numbers)
question_bank["15"] = question_15

def question_17():
    def insert_sorted_optimized(arr, num):

        insert_point = next((i for i, x in enumerate(arr) if x > num), len(arr))
        return arr[:insert_point] + [num] + arr[insert_point:]

    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
    num_to_insert = int(input("请输入一个整数，将会把它插入到已排序的数组中: "))
    new_sorted_array = insert_sorted_optimized(sorted_array, num_to_insert)
    print("插入后的新数组：", new_sorted_array)
question_bank["17"] = question_17

def question_19():
    def josephus_problem(n, k):

        people = list(range(1, n + 1))
        
        step = k - 1
        
        while len(people) > 1:
            index_to_remove = step % len(people)
            
            people.pop(index_to_remove)
        
        return people[0]

    n = 10  
    k = 3   
    result = josephus_problem(n, k)
    print(f"最后留下的是原来第{result}号的那位。")
question_bank["19"] = question_19